﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;
using DBUtility;
namespace TierApplication
{
    partial class RoleDao
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool CreateRole(RoleInfo role)
        {
            string Sql = string.Format("Insert Into P_Role(rolename,remark)Values('{0}','{1}')", role.RoleName,role.Remark);

            return DbHelperOra.ExecuteSql(Sql) > 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool ModifyRole(RoleInfo role)
        {
            string Sql = string.Format("Update P_Role  Set rolename = '{0}',remark = '{1}' Where roleid = '{2}'", role.RoleName,role.Remark, role.RoleId);
            return DbHelperOra.ExecuteSql(Sql) > 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public bool DeleteRole(string RoleId)
        {
            string sql = "Delete P_Role Where roleid = '" + RoleId + "'";
            return DbHelperOra.ExecuteSql(sql) > 0;
        }
        /// <summary>
        /// 取得所有角色
        /// </summary>
        /// <returns></returns>
        public List<RoleInfo> GetRoleAll()
        {
            List<RoleInfo> ListInfo = new List<RoleInfo>();
            RoleInfo info = null;

            using (DbDataReader reader = DbHelperOra.ExecuteReader("Select roleid, rolename,remark From P_Role"))
            {
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        info = new RoleInfo();
                        info.RoleId = (reader.IsDBNull(reader.GetOrdinal("roleid"))) ? int.Parse("0") : int.Parse(reader["roleid"].ToString());
                        info.RoleName = (reader.IsDBNull(reader.GetOrdinal("rolename"))) ? "" : reader["rolename"].ToString();
                        info.Remark = (reader.IsDBNull(reader.GetOrdinal("remark"))) ? "" : reader["remark"].ToString();
                        ListInfo.Add(info);
                    }
                }
            }
            return ListInfo;
        }
        /// <summary>
        /// 根据角色ID取得角色信息
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public RoleInfo GetRoleById(string RoleId)
        {
            RoleInfo info = new RoleInfo();

            using (DbDataReader reader = DbHelperOra.ExecuteReader("Select roleid, rolename,remark From P_Role where roleid='" + RoleId + "'"))
            {
                if (reader.Read())
                {
                    info.RoleId = (reader.IsDBNull(reader.GetOrdinal("roleid"))) ? int.Parse("0") : int.Parse(reader["roleid"].ToString());
                    info.RoleName = (reader.IsDBNull(reader.GetOrdinal("rolename"))) ? "" : reader["rolename"].ToString();
                    info.Remark = (reader.IsDBNull(reader.GetOrdinal("remark"))) ? "" : reader["remark"].ToString();
                }
            }
            return info;
        }
    }
}
